<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${packageName}<#if moduleName??>.${moduleName}</#if>.dao.${beanName}Dao">
    <resultMap id="${beanName}Entity" type="${packageName}<#if moduleName??>.${moduleName}</#if>.entity.${beanName}Entity">
        <id column="${tableConfiguration.keyColumn}" jdbcType="BIGINT" property="${tableConfiguration.keyProperty}" />
        <#list Fields as field>
        <result property="${field.javaField!''}" column="${field.field!''}"/>
        </#list>
    </resultMap>

    <sql id="Base_Columns">
    <#list Fields as field>
        ${field.field!''}<#if field_has_next>,</#if>
    </#list>
    </sql>

    <insert id="insert" keyColumn="${tableConfiguration.keyColumn}" keyProperty="${tableConfiguration.keyProperty}" parameterType="${packageName}<#if moduleName??>.${moduleName}</#if>.entity.${beanName}Entity" useGeneratedKeys="true">
        insert into ${tableName}
        <trim prefix="(" suffix=")" suffixOverrides=",">
        <#list Fields as field>
            <if test="${field.javaField!''} != null<#if field.javaType == "String"> and ${field.javaField!''} != '' </#if>">
            ${field.field!''}<#if field_has_next>,</#if>
            </if>
        </#list>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
        <#list Fields as field>
            <if test="${field.javaField!''} != null<#if field.javaType == "String"> and ${field.javaField!''} != '' </#if>">
             <#noparse>#{</#noparse>${field.javaField!''}<#noparse>}</#noparse><#if field_has_next>,</#if>
            </if>
        </#list>
        </trim>
    </insert>

    <update id="update" parameterType="${packageName}<#if moduleName??>.${moduleName}</#if>.entity.${beanName}Entity">
        update  ${tableName}
        <set>
            <#list Fields as field>
            <if test="${field.javaField!''} != null<#if field.javaType == "String"> and ${field.javaField!''} != '' </#if>">
                ${field.field!''}=<#noparse>#{</#noparse>${field.javaField!''}<#noparse>}</#noparse><#if field_has_next>,</#if>
            </if>
            </#list>
        </set>
        where ${tableConfiguration.keyColumn} = <#noparse>#{</#noparse>${tableConfiguration.keyProperty!''}<#noparse>}</#noparse>
    </update>

    <update id="logicalDelete" parameterType="${packageName}<#if moduleName??>.${moduleName}</#if>.entity.${beanName}Entity">
        update  ${tableName}
        <set>
         <#list Fields as field><#if field.field?index_of("_del") != -1>${field.field!''}=1,</#if></#list>
        <#list Fields as field>
            <#if field.field?index_of("_del") == -1>
            <if test="${field.javaField!''} != null<#if field.javaType == "String"> and ${field.javaField!''} != '' </#if>">
                ${field.field!''}=<#noparse>#{</#noparse>${field.javaField!''}<#noparse>}</#noparse><#if field_has_next>,</#if>
            </if>
            </#if>
        </#list>
        </set>
        where ${tableConfiguration.keyColumn} = <#noparse>#{</#noparse>${tableConfiguration.keyProperty!''}<#noparse>}</#noparse>
    </update>

    <select id="query" parameterType="${packageName}<#if moduleName??>.${moduleName}</#if>.entity.${beanName}Entity" resultMap="${beanName}Entity">
        select
        <include refid="Base_Columns" />
        from ${tableName}
        <where>
        <#list Fields as field>
            <#if field.field?index_of("_del") != -1 >
                ${field.field!''}=0
            </#if>
        </#list>
            <#list Fields as field>
            <#if field.queryMethods == "=">
            <if test="${field.javaField!''} != null<#if field.javaType == "String"> and ${field.javaField!''} != '' </#if>">
                and ${field.field!''}=<#noparse>#{</#noparse>${field.javaField!''}<#noparse>}</#noparse>
            </if>
            </#if>
            <#if field.queryMethods == "like">
            <if test="${field.javaField!''} != null<#if field.javaType == "String"> and ${field.javaField!''} != '' </#if>">
                and ${field.field!''} like concat(concat('%',<#noparse>#{</#noparse>${field.javaField!''}<#noparse>}</#noparse>),'%')
            </if>
            </#if>
            <#if field.queryMethods == "between">
            <if test="${field.javaField!''}Start != null<#if field.javaType == "String"> and ${field.javaField!''} != '' </#if>">
                and ${field.field!''} &gt;= <#noparse>#{</#noparse>${field.javaField!''}Start<#noparse>}</#noparse>
            </if>
            <if test="${field.javaField!''}End != null<#if field.javaType == "String"> and ${field.javaField!''} != '' </#if>">
                and ${field.field!''} &lt;= <#noparse>#{</#noparse>${field.javaField!''}End<#noparse>}</#noparse>
            </if>
            </#if>
            </#list>
        </where>
    </select>

    <select id="get" parameterType="${packageName}<#if moduleName??>.${moduleName}</#if>.entity.${beanName}Entity" resultMap="${beanName}Entity">
        select
        <include refid="Base_Columns" />
        from ${tableName}
        <where>
        <#list Fields as field>
            <#if field.field?index_of("_del") != -1 >
                ${field.field!''}=0
            </#if>
        </#list>
        <#list Fields as field>
            <#if field.queryMethods == "=">
            <if test="${field.javaField!''} != null<#if field.javaType == "String"> and ${field.javaField!''} != '' </#if>">
                and ${field.field!''}=<#noparse>#{</#noparse>${field.javaField!''}<#noparse>}</#noparse>
            </if>
            </#if>
            <#if field.queryMethods == "like">
            <if test="${field.javaField!''} != null<#if field.javaType == "String"> and ${field.javaField!''} != '' </#if>">
                and ${field.field!''} like concat(concat('%',<#noparse>#{</#noparse>${field.javaField!''}<#noparse>}</#noparse>),'%')
            </if>
            </#if>
            <#if field.queryMethods == "between">
            <if test="${field.javaField!''}Start != null<#if field.javaType == "String"> and ${field.javaField!''} != '' </#if>">
                and ${field.field!''} &gt;= <#noparse>#{</#noparse>${field.javaField!''}Start<#noparse>}</#noparse>
            </if>
            <if test="${field.javaField!''}End != null<#if field.javaType == "String"> and ${field.javaField!''} != '' </#if>">
                and ${field.field!''} &lt;= <#noparse>#{</#noparse>${field.javaField!''}End<#noparse>}</#noparse>
            </if>
            </#if>
        </#list>
        </where>
        limit 0,1
    </select>

    <select id="getById" parameterType="java.lang.Integer" resultMap="${beanName}Entity">
        select
        <include refid="Base_Columns"/>
        from ${tableName}
        where ${tableConfiguration.keyColumn} = <#noparse>#{</#noparse>${tableConfiguration.keyProperty!''}<#noparse>}</#noparse>
    </select>
</mapper>